﻿using System;
using System.Linq;
using System.Text.RegularExpressions;
using LitJson;

namespace Framework
{
    /// <summary>
    /// Object 类型转化
    /// </summary>
    public static class ObjectExtension
    {
        /// <summary>
        /// 安全的转化为String
        /// </summary>
        /// <param name="target"></param>
        /// <returns>string.Empty</returns>
        public static string SafeToString(this object target)
        {
            if (target == null)
            {
                return string.Empty;
            }
            return target.ToString();
        }

        #region 转化为Int
        /// <summary>
        /// 安全转化为Int
        /// </summary>
        /// <param name="target"></param>
        /// <returns>默认返回0</returns>
        public static int SafeToInt(this object target)
        {
            int tem;
            int.TryParse(target.SafeToString(), out tem);
            return tem;
        }
        /// <summary>
        /// 安全转化为Int16
        /// </summary>
        /// <param name="target"></param>
        /// <returns>默认返回0</returns>
        public static Int16 SafeToInt16(this object target)
        {
            Int16 tem;
            Int16.TryParse(target.SafeToString(), out tem);
            return tem;
        }
        /// <summary>
        /// 安全转化为Int32
        /// </summary>
        /// <param name="target"></param>
        /// <returns>默认返回0</returns>
        public static Int32 SafeToInt32(this object target)
        {
            Int32 tem;
            Int32.TryParse(target.SafeToString(), out tem);
            return tem;
        }
        /// <summary>
        /// 安全转化为Int64
        /// </summary>
        /// <param name="target"></param>
        /// <returns>默认返回0</returns>
        public static Int64 SafeToInt64(this object target)
        {
            Int64 tem;
            Int64.TryParse(target.SafeToString(), out tem);
            return tem;
        }
        #endregion

        /// <summary>
        /// 安全转化Long
        /// </summary>
        /// <param name="target"></param>
        /// <returns>默认返回0</returns>
        public static long SafeToLong(this object target)
        {
            long tem;
            long.TryParse(target.SafeToString(), out tem);
            return tem;
        }
        /// <summary>
        /// 转化安全Bool
        /// </summary>
        /// <param name="target"></param>
        /// <returns></returns>
        public static bool SafeToBool(this object target)
        {
            bool tem;
            bool.TryParse(target.SafeToString(), out tem);
            return tem;
        }
        /// <summary>
        /// 转化为DateTime
        /// </summary>
        /// <param name="target"></param>
        /// <returns></returns>
        public static DateTime SafeToDateTime(this object target)
        {
            DateTime tem;
            DateTime.TryParse(target.SafeToString(), out tem);
            return tem;
        }

        public static string Serialization(this object o)
        {
            return JsonMapper.ToJson(o);
        }
        #region Object 对象检测


        /// <summary>
        /// 判断对象是否为Int32类型的数字
        /// </summary>
        /// <returns></returns>
        public static bool IsNumeric(this object expression)
        {
            if (expression != null)
            {
                string str = expression.SafeToString();
                if (str.Length > 0 && str.Length <= 11 && Regex.IsMatch(str, @"^[-]?[0-9]*[.]?[0-9]*$"))
                {
                    if ((str.Length < 10) || (str.Length == 10 && str[0] == '1') || (str.Length == 11 && str[0] == '-' && str[1] == '1'))
                        return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 是否为Double类型
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        public static bool IsDouble(this object expression)
        {
            if (expression != null)
                return Regex.IsMatch(expression.ToString(), @"^([0-9])[0-9]*(\.\w*)?$");

            return false;
        }

        /// <summary>
        /// 判断给定的字符串数组(strNumber)中的数据是不是都为数值型
        /// </summary>
        /// <param name="strNumber">要确认的字符串数组</param>
        /// <returns>是则返加true 不是则返回 false</returns>
        public static bool IsNumericArray(this object[] strNumber)
        {
            if (strNumber == null)
                return false;
            if (strNumber.Length < 1)
                return false;
            return strNumber.All(id => id.IsNumeric());
        }
        #endregion

    }
}
